package api.business;


import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import common.BaseController;
import common.CommonResult;
import common.validateUser.InterfaceUsers;
import common.validateUser.ValidateUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.dictionary.ItemTypeServices;
import services.dictionary.MajorTypeServices;
import services.staff.StaffInfoServices;
import util.DateUtil;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.business.BusinessInfo;
import wt.staticData.ShortData;

import java.util.Date;

/**
 * Created by Administrator on 2017/8/16.
 */
@Controller
@RequestMapping("httpApi")
public class AddBusinessResultController extends BaseController {


    @Autowired
    public InterfaceUsers interfaceUsers;
    @Autowired
    ItemTypeServices itemTypeServices;
    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    StaffInfoServices staffInfoServices;
    @Autowired
    MajorTypeServices majorTypeServices;

    /**
     * 添加核查结果
     * @param uid
     * @param timeStamp
     * @param sign
     * @param businessCode
     * @param result
     * @param startTime
     * @param endTime
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "addBusinessResult", produces = "application/json;charset=UTF-8")
    public CommonResult addBusinessResult(
            String uid,
            Long timeStamp,
            String sign,
            String businessCode,
            String result,
            @DateTimeFormat(pattern = DateUtil.DATE_PATTEN) Date startTime,
            @DateTimeFormat(pattern = DateUtil.DATE_PATTEN) Date endTime
    ) throws Exception {


        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(uid, new NotBlankFluentValidator("uid"))
                .on(timeStamp, new NotNullFluentValidator("timeStamp"))
                .on(sign, new NotBlankFluentValidator("sign"))
                .on(businessCode, new NotBlankFluentValidator("businessCode"))
                .on(result, new NotBlankFluentValidator("result"))
                .on(startTime, new NotNullFluentValidator("startTime"))
                .on(endTime, new NotNullFluentValidator("endTime"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            CommonResult commonResult = new CommonResult();
            commonResult.addMessage(complexResult);
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }


        CommonResult commonResult = ValidateUserUtil.validateUser(uid, timeStamp, sign, interfaceUsers);  //用户验证
        if (!CommonResult.ZJ001.equals(commonResult.getCode())) {
            return commonResult;
        }




        BusinessInfo businessInfo = businessInfoServices.selectByBusinessCode(businessCode);
        if (businessInfo == null) {
            commonResult.setMessage(CommonResult.ZJ005_M+businessCode);
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }



        if (startTime.getTime() > endTime.getTime()) {
            commonResult.setMessage(CommonResult.ZJ005_M + "startTime/endTime");
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }


        businessInfo.setStartTime(startTime);
        businessInfo.setEndTime(endTime);
        businessInfo.setResult(result);
        businessInfo.setBusinessWorkFlow(ShortData.s3);

        businessInfoServices.updateByPrimaryKey(businessInfo);
        commonResult.setMessage(CommonResult.ZJ001_M);
        commonResult.setCode(CommonResult.ZJ001);
        return commonResult;
    }





}
